############ This Create App. Table 5 ################
library(data.table)
library(dplyr)
library(lubridate)
library(tidyr)
library(readstata13)
library(did)
library(ggplot2)
library(ggpubr)
library(ggrepel)
#Download latest version of did package and load it
#devtools::install_github("bcallaway11/did")
library(did)
library(here)
library(tidyverse)
library(scales)
source("R_Code/CS_Function.R")
set.seed(1234)




###############################################################################
#                            2011 and Onwards                                 #
###############################################################################


################## Load in the data ########################
data_analysis <- read.dta13("data/primary/state_panel_mw_poverty.dta") %>%
  filter(dem==1) %>%
  group_by(state_fips) %>% 
  mutate(eff100=ifelse( mw100==0,max(mw100*10000),year*mw100) %>% min()) %>% 
  mutate(eff75=ifelse( mw75==0,max(mw75*10000),year*mw75) %>% min()) %>% 
  mutate(eff50=ifelse( mw50==0,max(mw50*10000),year*mw50) %>% min()) %>% 
  mutate(eff25=ifelse( mw25==0,max(mw25*10000),year*mw25) %>% min()) %>% 
  mutate(dhpi=ifelse(chpi>=0.05,1,0)) %>%
  arrange(state_fips, year) %>%
  group_by(state_fips) %>%
  mutate(mw_change_nom = mw - lag(mw)) %>%
  mutate(mw_change_nom = ifelse(is.na(mw_change_nom),0,mw_change_nom)) %>%
  mutate(changed5 = ifelse(mw_change_nom >= 0.05, year, 9999)) %>%
  mutate(changed10 = ifelse(mw_change_nom >= 0.10, year, 9999)) %>%
  mutate(changed25 = ifelse(mw_change_nom >= 0.25, year, 9999)) %>%
  mutate(changed50 = ifelse(mw_change_nom >= 0.5, year, 9999)) %>%
  mutate(changed75 = ifelse(mw_change_nom >= 0.75, year, 9999)) %>%
  mutate(changed100 = ifelse(mw_change_nom >= 1.00, year, 9999)) %>%
  mutate(changed125 = ifelse(mw_change_nom >= 1.25, year, 9999)) %>%
  mutate(changed110 = ifelse(mw_change_nom >= 1.10, year, 9999)) %>%
  mutate(changed90 = ifelse(mw_change_nom >= 0.90, year, 9999)) %>%
  mutate(changed80 = ifelse(mw_change_nom >= 0.80, year, 9999)) %>%
  group_by(state_fips) %>% 
  mutate(eff_nom_5 = min(changed5)) %>%
  mutate(eff_nom_10 = min(changed10)) %>%
  mutate(eff_nom_25 = min(changed25)) %>%
  mutate(eff_nom_50 = min(changed50)) %>%
  mutate(eff_nom_75 = min(changed75)) %>%
  mutate(eff_nom_100 = min(changed100)) %>%
  mutate(eff_nom_80 = min(changed80)) %>%
  mutate(eff_nom_90 = min(changed90)) %>%
  mutate(eff_nom_110 = min(changed110)) %>%
  mutate(eff_nom_125 = min(changed125)) %>%
  mutate(y=under_100)


##################  RUN CS Estimates #############################

for (inc in c(10, 25, 50, 75, 100)) {
  tryCatch({
    label="MW"
    
    
    ## Table 3a: Control for smaller mn wage
    #lowerbound= (inc-25)/100
    lowerbound=0.25
    if (inc==25) { lowerbound=0.10 }
    if (inc==10) { lowerbound=0.05 }
    
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100 & treatyear==0, year, 9999)) %>%
      group_by(state_fips) %>%
      mutate(controlyear = min(controlyear)) %>%
      mutate(small = ifelse(year>=controlyear, 1,0)) %>%
      # filter(treatyear>0 | (treatyear==0 & small==0) )
      group_by(state_fips) %>%
      mutate(small = mean(small)) %>%
      mutate(small = ifelse(small>=0.5, 1,0)) 
    
    if (inc==10) {atts <- getatts()} else {atts <- getatts_small2()}
    agg_effects <- aggte(atts, type = "simple", na.rm=T)
    export1 <- attcoef()
    
    
    ### Table 3b: Drop smaller min wage
    data = data %>% filter(treatyear>0 | (treatyear==0 & year>controlyear) )
    atts <- getatts_small()
    agg_effects <- aggte(atts, type = "simple", na.rm=T)
    export2 <- attcoef()
    
    ## Export
    export = cbind(export1, export2[,2])
    write.csv(export, file=paste0("cs_estimates/att/att_", inc, "nom_overall_small", ".csv"), row.names=F, col.names=FALSE)
    rm(export, export1, export2)  
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}





###############################################################################
#                           Dube: 1984-2013                                   #
###############################################################################



################## Load in the data ########################
data_analysis <- read.dta13("data/primary/state_panel_mw_poverty_84.dta") %>% 
  filter(year<=2013) %>%
  group_by(state_fips) %>% 
  mutate(dhpi=ifelse(chpi>=0.05,1,0)) %>%
  arrange(state_fips, year) %>%
  group_by(state_fips) %>%
  mutate(mw_change_nom = mw - lag(mw)) %>%
  mutate(mw_change_nom = ifelse(is.na(mw_change_nom),0,mw_change_nom)) %>%
  mutate(changed5 = ifelse(mw_change_nom >= 0.05, year, 9999)) %>%
  mutate(changed10 = ifelse(mw_change_nom >= 0.10, year, 9999)) %>%
  mutate(changed25 = ifelse(mw_change_nom >= 0.25, year, 9999)) %>%
  mutate(changed50 = ifelse(mw_change_nom >= 0.5, year, 9999)) %>%
  mutate(changed75 = ifelse(mw_change_nom >= 0.75, year, 9999)) %>%
  mutate(changed100 = ifelse(mw_change_nom >= 1.00, year, 9999)) %>%
  mutate(changed125 = ifelse(mw_change_nom >= 1.25, year, 9999)) %>%
  mutate(changed110 = ifelse(mw_change_nom >= 1.10, year, 9999)) %>%
  mutate(changed90 = ifelse(mw_change_nom >= 0.90, year, 9999)) %>%
  mutate(changed80 = ifelse(mw_change_nom >= 0.80, year, 9999)) %>%
  group_by(state_fips) %>% 
  mutate(eff_nom_5 = min(changed5)) %>%
  mutate(eff_nom_10 = min(changed10)) %>%
  mutate(eff_nom_25 = min(changed25)) %>%
  mutate(eff_nom_50 = min(changed50)) %>%
  mutate(eff_nom_75 = min(changed75)) %>%
  mutate(eff_nom_100 = min(changed100)) %>%
  mutate(eff_nom_80 = min(changed80)) %>%
  mutate(eff_nom_90 = min(changed90)) %>%
  mutate(eff_nom_110 = min(changed110)) %>%
  mutate(eff_nom_125 = min(changed125)) %>%
  mutate(y=under_100)


##################  RUN CS Estimates #############################


for (inc in c(10, 25, 50, 75, 100)) {
  tryCatch({
    label="MW"
    

    ## Table 5a: Control for smaller mn wage
    #lowerbound = (inc-25)/100
    lowerbound=0.25
    if (inc==25) { lowerbound=0.10 }
    if (inc==10) { lowerbound=0.05 }
    
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      #mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100, year, 9999)) %>%
      mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100 & treatyear==0, year, 9999)) %>%
      group_by(state_fips) %>%
      mutate(controlyear = min(controlyear)) %>%
      mutate(small = ifelse(year>=controlyear, 1,0)) %>%
      #filter(treatyear>0 | (treatyear==0 & small==0) )  %>%
      group_by(state_fips) %>%
      mutate(small = mean(small)) %>%
      mutate(small = ifelse(small>=0.5, 1,0)) 
    
    atts <- getatts()
    agg_effects <- aggte(atts, type = "simple", na.rm=T)
    export1 <- attcoef()
    
    
    ### Table 4b: Drop smaller min wage
    data = data %>% filter(treatyear>0 | (treatyear==0 & year>controlyear) )
    atts <- getatts_small()
    agg_effects <- aggte(atts, type = "simple", na.rm=T)
    export2 <- attcoef()
    
    ## Export
    export = cbind(export1, export2[,2])
    write.csv(export, file=paste0("cs_estimates/att/att_", inc, "nom_overall_8413_small", ".csv"), row.names=F, col.names=FALSE)
    rm(export, export1, export2)
    
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}











###############################################################################
#                             Full: 1984+                                     #
###############################################################################



################## Load in the data ########################
data_analysis <- read.dta13("data/primary/state_panel_mw_poverty_84.dta") %>%
  group_by(state_fips) %>% 
  mutate(dhpi=ifelse(chpi>=0.05,1,0)) %>%
  arrange(state_fips, year) %>%
  group_by(state_fips) %>%
  mutate(mw_change_nom = mw - lag(mw)) %>%
  mutate(mw_change_nom = ifelse(is.na(mw_change_nom),0,mw_change_nom)) %>%
  mutate(changed5 = ifelse(mw_change_nom >= 0.05, year, 9999)) %>%
  mutate(changed10 = ifelse(mw_change_nom >= 0.10, year, 9999)) %>%
  mutate(changed25 = ifelse(mw_change_nom >= 0.25, year, 9999)) %>%
  mutate(changed50 = ifelse(mw_change_nom >= 0.5, year, 9999)) %>%
  mutate(changed75 = ifelse(mw_change_nom >= 0.75, year, 9999)) %>%
  mutate(changed100 = ifelse(mw_change_nom >= 1.00, year, 9999)) %>%
  mutate(changed125 = ifelse(mw_change_nom >= 1.25, year, 9999)) %>%
  mutate(changed110 = ifelse(mw_change_nom >= 1.10, year, 9999)) %>%
  mutate(changed90 = ifelse(mw_change_nom >= 0.90, year, 9999)) %>%
  mutate(changed80 = ifelse(mw_change_nom >= 0.80, year, 9999)) %>%
  group_by(state_fips) %>% 
  mutate(eff_nom_5 = min(changed5)) %>%
  mutate(eff_nom_10 = min(changed10)) %>%
  mutate(eff_nom_25 = min(changed25)) %>%
  mutate(eff_nom_50 = min(changed50)) %>%
  mutate(eff_nom_75 = min(changed75)) %>%
  mutate(eff_nom_100 = min(changed100)) %>%
  mutate(eff_nom_80 = min(changed80)) %>%
  mutate(eff_nom_90 = min(changed90)) %>%
  mutate(eff_nom_110 = min(changed110)) %>%
  mutate(eff_nom_125 = min(changed125)) %>%
  mutate(y=under_100)


##################  RUN CS Estimates #############################
for (inc in c(10,25,50,75, 100)) {
  #for (inc in c(100)) {
  tryCatch({
    label="MW"
    
    ## Table 5a: Control for smaller min wage
    #lowerbound = (inc-25)/100
    lowerbound=0.25
    if (inc==25) { lowerbound=0.10 }
    if (inc==10) { lowerbound=0.05 }
    
    data <- data_analysis %>% mutate(treatyear = get(paste0("eff_nom_", inc))) %>% 
      mutate(treatyear = ifelse(treatyear==9999, 0, treatyear)) %>%
      #mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100, year, 9999)) %>%
      mutate(controlyear = ifelse(mw_change_nom >= lowerbound & mw_change_nom < inc/100 & treatyear==0, year, 9999)) %>%
      group_by(state_fips) %>%
      mutate(controlyear = min(controlyear)) %>%
      mutate(small = ifelse(year>=controlyear, 1,0)) %>%
      #filter(treatyear>0 | (treatyear==0 & small==0) )  %>%
      group_by(state_fips) %>%
      mutate(small = mean(small)) %>%
      mutate(small = ifelse(small>=0.5, 1,0)) 
    
    if (inc>50) {atts <- getatts_small2()} else {atts <- getatts()}
    agg_effects <- aggte(atts, type = "simple", na.rm=T)
    export1 <- attcoef()
    
    
    ### Table 5b: Drop smaller min wage
    data = data %>% filter(treatyear>0 | (treatyear==0 & year>controlyear) )
    atts <- getatts_small()
    agg_effects <- aggte(atts, type = "simple", na.rm=T)
    export2 <- attcoef()
    
    ## Export
    export = cbind(export1, export2[,2])
    write.csv(export, file=paste0("cs_estimates/att/att_", inc, "nom_overall_84_small", ".csv"), row.names=F, col.names=FALSE)
    rm(export, export1, export2)
    
  }, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}






